<?php

use App\Models\DawnUser;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\HandleInertiaRequests;
use App\Http\Middleware\HttpLoggingMiddleware;
use App\Exceptions\Handler;
use Illuminate\Console\Scheduling\Schedule;
return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withCommands([
        
    ])
    ->withSchedule(function (Schedule $schedule) {
        // 使用ScheduleService进行动态任务调度
        app(\App\Services\ScheduleServiceInterFace::class)->registerTasks($schedule);
        
        // 保留一些默认任务作为备用
        // 方式一：直接定义一个简单的任务
        // $schedule->call(function () {
        //     Log::info('备用任务执行 - ' . now()->toDateTimeString());
        // })->cron('0 */6 * * *')->name('backup-task'); // 每6小时执行一次
    })
    ->withMiddleware(function (Middleware $middleware) {
        // 没有登录 转到登录进行登录
        $middleware->redirectGuestsTo('/user/login');
        //
        $middleware->web(append: [
            HandleInertiaRequests::class,
            HttpLoggingMiddleware::class,
        ]);
        $middleware->validateCsrfTokens(except: [
            '*', // 排除所有路由的CSR的防护
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->reportable(function (Throwable $e) {
            //
        });

        $exceptions->dontReport([
            //
        ]);

        $exceptions->render(function (Throwable $e) {
            return app(Handler::class)->render(request(), $e);
        });
    })->create();
